package com.example.lzw.fragmenttest303lzw.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lzw.fragmenttest303lzw.R;
import com.example.lzw.fragmenttest303lzw.adapter.MyAdapter;
import com.example.lzw.fragmenttest303lzw.bean.TcaseBean;
import com.example.lzw.fragmenttest303lzw.iface.TcaseListener;
import com.example.lzw.fragmenttest303lzw.model.TcaseModel;

import java.util.List;

/**
 * Created by Neusoft on 2017/4/9.
 *
 * fragment结合recyclerView
 */

public class Fragment1 extends Fragment {

    private View view=null;
    private Context context;
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;//显示效果
    private MyAdapter adapter;
    private List<TcaseBean> list;
    TcaseListener tcaseListener = new TcaseListener() {
        @Override
        public void onResponse(List<TcaseBean> beanList) {
            list = beanList;
            adapter.setList(list);
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onFile(String msg) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context=getActivity();
       //动态加载Fragment1的布局文件
        view=inflater.inflate(R.layout.fragment1,container,false);

        recyclerViewinit();
        TcaseModel tcaseModel = new TcaseModel();
        tcaseModel.getResultList("tcase", 4, "rv1l2tidbomann2qn86nln6ur2", tcaseListener);
        //tvinfo.setText("微信");
        //注意Toast第一个参数上下文
        Toast.makeText(context, "加载Fragment微信", Toast.LENGTH_SHORT).show();
        //返回动态生成的view
        return view;
    }

    private void recyclerViewinit() {
        //获取布局上的控件，注意通过view获取
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);

        layoutManager = new LinearLayoutManager(context);

        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);

        adapter = new MyAdapter(context);
        adapter.setList(list);
        recyclerView.setAdapter(adapter);
    }
}
